<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Exceptions\InvalidRequestException;
use App\Models\Purchase;
use App\Models\Warehouse;
use App\Models\WarehouseIn;
use App\Models\WarehouseOut;
use App\Models\WarehouseRma;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class WarehouseService
{
    public function get($title)
    {
        $sql = Warehouse::when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('name','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function ins($title)
    {
        $sql = WarehouseIn::when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('name','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function outs($title)
    {
        $sql = WarehouseIn::when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('name','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function rmas($title)
    {
        $sql = WarehouseRma::when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('name','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function insertIn($id){
        $res = Purchase::where('id',$id)->first()->toArray();
        try {
            $data = DB::transaction(function () use ($res) {
                // $Warehouse = Warehouse::where('encoding',$res['encoding'])->where('number',$res['number'])->where('type',$res['type'])->where('company_id',$res['company_id'])->where('project_id',$res['project_id'])->first();
                // if(!$Warehouse){
                //     $Warehouse = new Warehouse();
                //     $Warehouse->company_id = $res['company_id'];
                //     $Warehouse->project_id = $res['project_id'];
                //     $Warehouse->admin_id = $res['admin_id'];
                //     $Warehouse->inventory = $res['quantity'];
                //     $Warehouse->name = $res['name'];
                //     $Warehouse->encoding = $res['encoding'];
                //     $Warehouse->number = $res['number'];
                //     $Warehouse->specification = $res['specification'];
                //     $Warehouse->unit_price = $res['unit_price'];
                //     $Warehouse->type = $res['type'];
                // }else{
                //     $Warehouse->inventory = $Warehouse->inventory + $res['quantity'];
                // }
                // $Warehouse->save();
                $data = WarehouseIn::create($res);
                return $data->id;
            });
            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function confirm($id){
        $res = WarehouseIn::where('id',$id)->first();
        if(!$res){
            throw new InvalidRequestException('入库材料不存在');
        }
        if($res->status == 1){
            throw new InvalidRequestException('该材料已入库');
        }
        try {
            $data = DB::transaction(function () use ($res) {
                $Warehouse = Warehouse::where('encoding',$res->encoding)->where('number',$res->number)->where('type',$res->type)->where('company_id',$res->company_id)->where('project_id',$res->project_id)->first();
                if(!$Warehouse){
                    $Warehouse = new Warehouse();
                    $Warehouse->company_id = $res->company_id;
                    $Warehouse->project_id = $res->project_id;
                    $Warehouse->admin_id = $res->admin_id;
                    $Warehouse->inventory = $res->quantity;
                    $Warehouse->name = $res->name;
                    $Warehouse->encoding = $res->encoding;
                    $Warehouse->number = $res->number;
                    $Warehouse->specification = $res->specification;
                    $Warehouse->unit_price = $res->unit_price;
                    $Warehouse->type = $res->type;
                }else{
                    $Warehouse->inventory = $Warehouse->inventory + $res->quantity;
                }
                $Warehouse->save();
                $res->status = 1;
                $res->save();
                return $Warehouse->id;
            });
            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function updateIn($input){
        if($input['delivery_date']){
            $input['delivery_date'] = Date("Y-m-d",$input['delivery_date'] / 1000);
        }

        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = WarehouseIn::where('id',$input['id'])->update($res);
            if($data){
                return $input['id'];
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function insertOut($input){
        if($input['draw_date']){
            $input['draw_date'] = Date("Y-m-d",$input['draw_date'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $data = DB::transaction(function () use ($res) {
                $Warehouse = Warehouse::where('id',$res['warehouse_id'])->where('company_id',$res['company_id'])->where('project_id',$res['project_id'])->first();
                if(!$Warehouse){
                    throw new InvalidRequestException('请选择要出库的材料');
                }
                if($Warehouse->inventory < $res['quantity']){
                    throw new InvalidRequestException('库存不足');
                }
                $Warehouse->inventory = $Warehouse->inventory - $res['quantity'];
                $Warehouse->save();
                $data = WarehouseOut::create($res);
                //添加到库存
                return $data->id;
            });

            return $data;

        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }


    public function insertRma($input){
        if($input['rma_date']){
            $input['rma_date'] = Date("Y-m-d",$input['rma_date'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $data = DB::transaction(function () use ($res) {
                $Warehouse = Warehouse::where('id',$res['warehouse_id'])->where('company_id',$res['company_id'])->where('project_id',$res['project_id'])->first();
                if(!$Warehouse){
                    throw new InvalidRequestException('请选择要退货的材料');
                }
                if($Warehouse->inventory < $res['quantity']){
                    throw new InvalidRequestException('库存不足');
                }
                $Warehouse->inventory = $Warehouse->inventory - $res['quantity'];
                $Warehouse->save();
                $data = WarehouseRma::create($res);
                //添加到库存
                return $data->id;
            });
            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
}